import { useRoute } from 'vue-router'
import { ref } from 'vue'
import { imgClassif360, searchImg360, hotSearch } from '@/api/index'
import { message } from 'ant-design-vue'
import { setLocalData } from '@/utils/tool'
export default () => {
  const keyword: any = ref('')
  const $route = useRoute()
  const classif: any = ref([])
  const count = ref(10)
  const page = ref(1)
  const hots: any = ref([])
  const total = ref(0)
  // 搜索壁纸
  const getSearch = async (start: number, limit: number) => {
    page.value = start
    count.value = limit
    classif.value = []
    const obj = {
      c: 'WallPaper',
      a: 'search',
      kw: keyword.value,
      start: (start - 1) * limit,
      count: limit,
    }
    const result = await searchImg360(obj)
    classif.value = result.data
    total.value = result.total * 1
  }
  // 今日热搜
  const todayHotSearch = async () => {
    const result = await hotSearch()
    hots.value = result.data
  }
  const clickKeyword = (key: string) => {
    keyword.value = key
    getSearch(1, 10)
  }

  // 获取壁纸
  const getImg = async (start: number, limit: number) => {
    classif.value = []
    const obj = {
      c: 'WallPaper',
      a: 'getAppsByCategory',
      cid: $route.query.id,
      start: (start - 1) * limit,
      count: limit,
      from: '360chrome',
    }
    const result = await imgClassif360(obj)
    classif.value = result.data
    total.value = result.total * 1 >= 1000 ? 1000 : result.total * 1
  }

  // 标签样式
  const computerTag = (tags: any) => {
    let arr: any = []
    if (tags) {
      arr = tags.split(' ')
    }
    return arr
  }
  const colorMap: { [key: number]: string } = {
    0: 'blue',
    1: 'orange',
    2: 'green',
    3: 'cyan',
    4: 'red',
    5: 'purple',
    6: 'pink',
    7: 'blue',
    8: 'orange',
    9: 'green',
    10: 'cyan',
    11: 'red',
    12: 'purple',
    13: 'pink',
  }
  const computerColor = (i: number): string => {
    return colorMap[i] || 'blue'
  }

  // 返回顶部
  const backTop = (num: any) => {
    window.scrollTo({
      top: num,
      behavior: 'smooth',
    })
  }

  // 设置背景图
  const setBackImg = (src: any) => {
    const url = `url(${src})`
    setLocalData('LOGIN_IMG', url)
    message.success('设置成功！')
  }

  return {
    page,
    count,
    classif,
    keyword,
    $route,
    hots,
    total,
    getSearch,
    todayHotSearch,
    getImg,
    computerTag,
    computerColor,
    setBackImg,
    backTop,
    clickKeyword,
  }
}
